package com.tz.farmer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.model.FarmerSummary;
import com.tz.farmer.model.FarmerSunData;
import com.tz.farmer.service.IFarmerSummaryService;
import com.tz.farmer.service.IFarmerSunDataService;
import com.tz.farmer.service.ISummaryService;
import com.tz.farmer.so.DefaultSo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
@RestController
@RequestMapping("/market")
@Api(tags = "阳光市场接口")
public class MarketController {

    private static final String marketDataKey = "sunMarket";
    private static final String normalDataKey = "sunNormal";
    private static final String fastDataKey = "sunFast";
    private static final String market = "阳光市场";
    private static final String channel = "抽检";

    @Autowired
    private IFarmerSunDataService dataService;

    @Autowired
    private ISummaryService summaryService;

    @Autowired
    private IFarmerSummaryService farmerSummaryService;

    @GetMapping("/summary")
    @ApiOperation(value = "阳光市场-汇总")
    public ResultEx summary() {
        Map<String, Object> response = new TreeMap<>();

        // 创建查询阳光市场数量表达式
        QueryWrapper<FarmerSunData> wrapper = new QueryWrapper<FarmerSunData>()
            .eq("market", market).eq("code", marketDataKey)
            .select("value");

        List<Object> listObjs = dataService.listObjs(wrapper);

        response.put("market", listObjs.stream().findFirst().orElse(0));
        response.put("normal", summaryService.getSummary(market, channel, normalDataKey));
        response.put("fast", summaryService.getSummary(market, "快检", fastDataKey));

        return ResultEx.builder().makeSuccessResult(response);
    }

    @PostMapping("/set-market")
    @ApiOperation(value = "设置阳光市场数量")
    public ResultEx setMarkets(@RequestBody DefaultSo body) {
        if (ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少任务总数");
        }

        if (dataService.setData(market, marketDataKey, body.getCount())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-normal")
    @ApiOperation(value = "设置抽检总任务")
    public ResultEx setNormalTotalTask(@RequestBody DefaultSo body) {
        if (ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少任务总数");
        }

        if (dataService.setData(market, normalDataKey, body.getCount())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-fast")
    @ApiOperation(value = "设置快检总任务")
    public ResultEx setFastTotalTask(@RequestBody DefaultSo body) {
        if (ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少任务总数");
        }

        if (dataService.setData(market, fastDataKey, body.getCount())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/add-check-normal")
    @ApiOperation(value = "添加抽检子项")
    public ResultEx addSummaryByNormal(@RequestBody @Validated FarmerSummary body) {
        if (body != null) body.setChannel(channel);
        if (body != null) body.setMarket(market);
        // 验证是否更新成功
        if (farmerSummaryService.saveOrUpdate(body)) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/add-check-fast")
    @ApiOperation(value = "添加快检子项")
    public ResultEx addSummaryByFast(@RequestBody @Validated FarmerSummary body) {
        if (body != null) body.setChannel("快检");
        if (body != null) body.setMarket(market);
        // 验证是否更新成功
        if (farmerSummaryService.saveOrUpdate(body)) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }
}
